package com.game.doudizhu.repository;

import com.game.doudizhu.model.Friendship;
import com.game.doudizhu.model.Player;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FriendshipRepository extends JpaRepository<Friendship, Long> {
    
    boolean existsByPlayerAndFriendAndStatus(Player player, Player friend, String status);
    
    @Query("SELECT f.friend FROM Friendship f WHERE f.player = :player AND f.status = :status")
    List<Player> findFriendsByPlayerAndStatus(Player player, String status);
    
    List<Friendship> findByFriendAndStatus(Player friend, String status);
    
    void deleteByPlayerAndFriendAndStatus(Player player, Player friend, String status);

    Friendship findByPlayerAndFriend(Player sender, Player receiver);

    @Modifying
    @Query("DELETE FROM Friendship f WHERE f.player.id = :playerId OR f.friend.id = :friendId")
    void deleteByPlayerIdOrFriendId(@Param("playerId") Long playerId, @Param("friendId") Long friendId);
}